/*
 * @Author: silence
 * @Date: 2022-08-08 13:48:51
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-08-12 09:55:50
 * @Description: 权限相关
 */


import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

import '../../main.dart';
import '../Toast/MyTosat.dart';

class PermissionUtils {
  /**
   * 相机权限
   */
  static Future<bool> permission_camera() async {
    PermissionStatus status = await Permission.camera.request();
    if (status == PermissionStatus.granted) {
      return true;
    }

    if (status == PermissionStatus.denied) {
      MyToast().show("相机权限已关闭,请在手机设置中打开");
    }
    return false;
  }

  /**
   *  相册权限
   */
  static Future<bool> permission_photo() async {
    if (Platform.isAndroid) {
      PermissionStatus storageStatus = await Permission.storage.request();
      if (storageStatus == PermissionStatus.granted) {
        return true;
      }
      MyToast().show("储存权限已关闭,请在手机设置中打");
    } else {
      PermissionStatus status = await Permission.photos.status;
      if (status == PermissionStatus.granted) {
        return true;
      }
      MyToast().show("相册权限已关闭,请在手机设置中打开");
    }
    return false;
  }

  /**
   * 推送权限
   */
  static Future<bool> permission_notification() async {
    PermissionStatus status = await Permission.notification.request();
    if (status == PermissionStatus.granted) {
      return true;
    }
    // tipsView("通知权限已关闭,请在手机设置中打开");
    return false;
  }

  ///检查读写权限具体情况
  ///
  static Future<bool> checkReadWritePermission() async {
    if (Platform.isIOS) {
      //iOS平台不需要
      return true;
    }

    //获取当前的权限
    PermissionStatus status = await Permission.storage.status;
    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else if (status.isDenied) {
      // MyToast.show("文件储存权限已关闭");
      //MyToast().show("文件储存权限已关闭，您将不能下载，是否去开启？");
    } else {
      //未授权则发起一次申请
      status = await Permission.storage.request();
    }
    return false;
  }

  ///安装app权限
  static Future<bool> checkInstallAppPermission() async {
    var status = await Permission.requestInstallPackages.status;
    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.requestInstallPackages.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        MyToast().show("权限未开启");
        return false;
      }
    }
  }
}
